package com.qf.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.pojo.User;
import com.qf.service.EsService;
import com.qf.service.UserService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class EsServicerImpl implements EsService {

    @Autowired
    private RestHighLevelClient highLevelClient;
    //lazy啊啊啊
    @Autowired
    private UserService userService;

    /*SpringIOC容器中没有，之前是自己new的
    * ObjectMapper objectMapper=new ObjectMapper;
    * 在这里我们在配置文件里创建ObjectMapper；然后直接注入
    * */
    @Autowired
    private ObjectMapper objectMapper;



    @Override
    public void createIndexUser(String user) {


        try {
            highLevelClient.indices().create(
                    new CreateIndexRequest(user)
                            .settings(Settings.builder()
                                    .put("number_of_shards",5)
                                    .put("number_of_replicas",1))
                            /*
                            *
                            *       private Integer id;
                                    private String name;
                                    private String password;
                                    private String sex;
                                    private Date birthday;
                                    private Date registTime;*/
                            .mapping(JsonXContent.contentBuilder()
                                            .startObject()
                                            .startObject("properties")

                                            .startObject("name")
                                            .field("type", "text")
                                            .endObject()
                                            .startObject("password")
                                            .field("type", "keyword")
                                            .endObject()
                                            .startObject("sex")
                                            .field("type", "keyword")
                                            .endObject()
                                            .startObject("birthday")
                                            .field("type", "date")
                                            .field("format", "yyyy-MM-dd")
                                            .endObject()
                                            .startObject("registTime")
                                            .field("type", "date")
                                            .field("format", "yyyy-MM-dd HH:mm:ss")
                                            .endObject()

                                            .endObject()
                                            .endObject())
                    , RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /*批量导入数据，批量对象确定具体索引user*/
    @Override
    public void importIndexUser(String user) {
        List<User> userList = userService.findAll();
        BulkRequest bulkRequest = new BulkRequest(user);

        try {
            for (User u : userList) {
                bulkRequest.add(new IndexRequest().source(objectMapper.writeValueAsString(u),XContentType.JSON).id(String.valueOf(u.getId())));
            }
            //一次性提交所有的数据
            BulkResponse bulk = highLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
            System.out.println(bulk.status());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
